import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    hasLogin: false,
    token: "",
    userInfo: {},
    indexInfo: {},
    adress: "",
    shareid: "",
    sharetime: "",
  },
  mutations: {
    getadress(state, data) {
      state.adress = data
    },
    changTime(state, data) {
      state.sharetime = data
    },
    changShareid(state, data) {
      state.shareid = data;
      uni.setStorage({ //缓存用户登陆状态
        key: 'shareid',
        data: data
      })
    },
    removeShareid(state, data) {
      state.shareid = '';
      uni.removeStorage({ //缓存用户登陆状态
        key: 'shareid',
      })
    },
    changIndex(state, data) {
      state.indexInfo = data
    },
    changeperson(state, type) {
      Vue.prototype.$ComFunJS.ajax({
        url: "/kkev/mobile/u/user/queryUser",
        method: 'post',
        success: function(res) {
          if (res.retCode == 200) {
            state.userInfo = res.data;
          } else {
            state.hasLogin = false;
            state.token = "";
            state.userInfo = {};
            uni.removeStorage({
              key: 'token'
            });
            Vue.prototype.$ComFunJS.token = "";
          }
        }
      })
    },
    login(state, provider) {
      state.hasLogin = true;
      state.token = provider.token;
      state.userInfo = provider;
      Vue.prototype.$ComFunJS.token = provider.token;
      uni.setStorage({ //缓存用户登陆状态
        key: 'token',
        data: provider
      })
    },
    logout(state) {
      state.hasLogin = false;
      state.token = "";
      state.userInfo = {};
      uni.removeStorage({
        key: 'token'
      });
      Vue.prototype.$ComFunJS.token = "";
    }
  },
  actions: {

  }
})

export default store